<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>浏览 - MdbSystem</title>

    <!-- MathJax -->
    <script>
    MathJax = {
        tex: {
            inlineMath: [['$', '$'], ['\\(', '\\)']],
            displayMath: [['$$', '$$'], ['\\[', '\\]']]
        },
        svg: {
            fontCache: 'global'
        },
        startup: {
            typeset: false
        }
    };
    </script>
    <script src="https://cdn.jsdelivr.net/npm/mdui@2.1.3/mdui.global.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/mdui@2.1.3/mdui.min.css">
    <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script>

    <!-- Markdown 解析 -->
    <script src="https://cdn.jsdelivr.net/npm/showdown/dist/showdown.min.js"></script>

    <style>
        /* 按钮定位 */
        .button-container {
            position: fixed;
            bottom: 30px;
            right: 30px;
            display: flex;
            flex-direction: column;
            z-index: 1000;
        }

        .mdui-btn-edit-page {
          background-color: #FFC107;
          color: white;
          border-radius: 4px;
          padding: 10px 20px;
          margin-top: 20px;
          border: none;
          cursor: pointer;
          transition: background-color 0.3s ease;
        }
        .mdui-btn-edit-page:hover {
          background-color: #FFA000;
        }
        .mdui-btn-delete-page {
          background-color: #f44336;
          color: white;
          border-radius: 4px;
          padding: 10px 20px;
          margin-top: 20px;
          border: none;
          cursor: pointer;
          transition: background-color 0.3s ease;
        }
        .mdui-btn-delete-page:hover {
          background-color: #d32f2f;
        }
        .title {
            font-size: 24px;
            font-weight: bold;
            color: #007bff;
            margin-bottom: 10px;
        }
        .category {
            font-size: 16px;
            color: #0056b3;
            margin-bottom: 20px;
        }
        .tag {
            background-color: #e0f7fa;
            color: #007bff;
            padding: 5px 10px;
            border-radius: 5px;
            font-size: 14px;
        }
        .tags {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }
        .container {
            display: flex;
            gap: 20px;
            padding: 20px;
            max-width: 1600px;
            margin: 0 auto;
        }
        #display {
            flex: 1;
            min-height: 80vh;
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow-y: auto;
        }
        .markdown {
            line-height: 1.6;
            color: #333;
        }
        .markdown h1 {
            font-size: 2em;
            border-bottom: 2px solid #eee;
            padding-bottom: 0.3em;
        }
        .markdown h2 { font-size: 1.75em; }
        .markdown h3 { font-size: 1.5em; }
        .markdown pre {
            background: #f8f8f8;
            padding: 1em;
            border-radius: 5px;
            overflow-x: auto;
        }
        .markdown code {
            font-family: 'SFMono-Regular', Consolas, monospace;
            background: #f8f8f8;
            padding: 0.2em 0.4em;
            border-radius: 3px;
        }
        .markdown blockquote {
            border-left: 4px solid #ddd;
            padding-left: 1em;
            color: #666;
            margin: 1em 0;
        }
        .markdown table {
            border-collapse: collapse;
            width: 100%;
            margin: 1em 0;
        }
        .markdown th,
        .markdown td {
            border: 1px solid #ddd;
            padding: 8px;
        }
        .markdown th {
            background-color: #f8f8f8;
        }
    </style>
</head>
<body>
    <div class="container">
        <textarea id="editor" style="display:none" placeholder="请输入Markdown内容..."></textarea>
        <div id="display" class="markdown"></div>
    </div>

    <!-- 按钮容器 -->
    <div class="button-container">
        <button class="mdui-btn-edit-page" onclick="goToEdit()"><b>✒️ 编辑</b></button>
        <button class="mdui-btn-delete-page" onclick="deletePage()"><b>🗑️ 删除</b></button>
    </div>

    <mdui-dialog
  close-on-overlay-click
  headline="确认删除？"
  class="confirm"
>
  <mdui-button slot="action" variant="text" onclick="document.querySelector('.confirm').open = false">取消</mdui-button>
  <mdui-button slot="action" variant="tonal" onclick="deleteRequest()">确认</mdui-button>
</mdui-dialog>

    <script>
        const urlParams = new URLSearchParams(window.location.search);
        const id = urlParams.get('id');
        function goToEdit(){
            window.location.href = `/edit?id=${id}`;
        }

        function deletePage(){
            // Delete功能(请求发包以及后续鉴权？暂时搞不定，先丢这吧。)
            const dialog = document.querySelector(".confirm");
            dialog.open = true;
        }

        function deleteRequest(){
                      fetch(`/delete?id=${id}`)
                      .then(response => {
                        if (!response.ok) throw new Error('网络响应异常');
                        return response.json();
                    })
                    .then(data => {
                        if (data.error_code == 1){
                        showError("内容不存在");
                        setTimeout(() => {
                            window.location.href = '/';
                        }, 3000);
                        }
                        if (data.error_code == 0){
                        showError('删除成功！');
                        setTimeout(() => {
                            window.location.href = '/';
                        }, 2000);
                    }
                    })
                  }


        function showError(message) {
            mdui.snackbar({
                message: message,
                position: 'right-top',
                timeout: 3000
            });
        }

        const render = new showdown.Converter({
            tables: true,
            simplifiedAutoLink: true,
            strikethrough: true,
            tasklists: true
        });

        if (!id) {
            showError('无效的文章ID');
        } else {
            fetch(`/detailData?id=${id}`)
            .then(response => {
                if (!response.ok) throw new Error('网络响应异常');
                return response.json();
            })
            .then(data => {
                if (data.error_code == 1){
                    showError("文章不存在");
                    setTimeout(() => {
                            window.location.href = '/';
                        }, 2000);
                }
                const title = data.title;
                const category = data.category;
                const tags = data.tags;
                const content = data.content;
                const html = render.makeHtml(content);

                display.innerHTML = `
                    <div class="title" id="title">${title}</div>
                    <div class="category" id="category">${category}</div>
                    <div class="tags" id="tags"></div>
                    ${html}
                `;

                const tagsContainer = document.getElementById('tags');
                if (data.tags && data.tags.trim()) {
                    data.tags.split(' ').forEach(tag => {
                        const tagElement = document.createElement('div');
                        tagElement.className = 'tag';
                        tagElement.textContent = tag;
                        tagsContainer.appendChild(tagElement);
                    });
                } else {
                    tagsContainer.innerHTML = '<div>暂无标签</div>';
                }
            });
        }

        MathJax.typesetPromise([display]).catch(err => {
            console.log('公式渲染错误:', err);
        });
    </script>
</body>
</html>